// pages/association/association.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    actTab: '1',
    leftpx: '0rpx',
    url: app.data.imgUrl,
    asslist: [],
    recommonList: [], //推荐
    params: { //社团图片55开
      page: 1,
      numPerPage: 10
    },
    recommonParams: {//推荐列表分页
      page: 1,
      numPerPage: 10
    },
    startX: 0,
    startY: 0,
    margintop: 0,
    isTop: true, //是否到顶部
    isLoodingHide: true//加载动画
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    this.refirshMoreList()
    this.refirshMoreRecomend()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    //同步一级评论数据
    if (app.globalData.oneSynchroData) {
      this.updataInfoList(app.globalData.oneSynchroData)
    }
  },
  //遍历infoList，修改点赞数，状态
  updataInfoList(oneSynchroData) {
    for (var item in this.data.recommonList) {
      if (this.data.recommonList[item].id == app.globalData.oneSynchroData.id) {
        console.log(this.data.recommonList[item])
        this.data.recommonList[item].upNum += app.globalData.oneSynchroData.dainzan
        if (this.data.recommonList[item].upStatus == 1 && app.globalData.oneSynchroData.dainzan < 0) {
          this.data.recommonList[item].upStatus = 0
        } else if (this.data.recommonList[item].upStatus == 0 && app.globalData.oneSynchroData.dainzan > 0) {
          this.data.recommonList[item].upStatus = 1
        }
        this.data.recommonList[item].commentNum = app.globalData.oneSynchroData.oneCommentNum
      }
    }
    this.setData({
      recommonList: this.data.recommonList
    })
    app.globalData.oneSynchroData = null
  },
 
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },
  //社团滚动置顶事件
  refirshMoreList() {
    this.data.params.page = 1
    this.getList(this.data.params, true)
  },
  //推荐滚动置顶事件
  refirshMoreRecomend() {
    this.data.recommonParams.page = 1
    this.getRecomend(this.data.recommonParams, true)
  },
  //推荐滚动触底事件
  loadMoreRecomend() {
    this.setData({
      isLoodingHide:false
    })
    this.data.recommonParams.page += 1
    this.getRecomend(this.data.recommonParams)
  },
  //tabbar点击事件
  tabChange(e) {
    let id = e.currentTarget.dataset.id
    if (id == '1') {
      this.setData({
        actTab: id,
        leftpx: '0rpx'
      })
    } else {
      this.setData({
        actTab: id,
        leftpx: '357rpx'
      })
    }
  },
  //手指向下滑动，记录Y起点坐标
  touchstart(e) {
    this.setData({
      startX: e.changedTouches[0].clientX,
      startY: e.changedTouches[0].clientY,
    })
  },
  touchmove(e) {
    var that = this
    var startX = this.data.startX //开始X坐标
    var startY = this.data.startY //开始Y坐标
    var touchmoveX = e.changedTouches[0].clientX //滑动变化坐标X
    var touchmoveY = e.changedTouches[0].clientY //滑动变化坐标Y
    var angle = that.angle({
      X: startX,
      Y: startY
    }, {
      X: touchmoveX,
      Y: touchmoveY
    });

    if (Math.abs(angle) < 50) return;
    this.setData({
      margintop: this.data.margintop + 10
    })
  },
  //手指触摸动作结束
  touchend(e) {
    if (this.data.margintop > 50 && this.data.isTop) {
      wx.showLoading({
        title: '正在刷新中...',
      })
      this.data.margintop = 51
      this.refirsh()
      setTimeout(() => {
        wx.hideLoading()
      }, 1000)
    }
  },
  //滚动到顶部事件
  scrolltoupper(e) {
    this.data.isTop = true
  },
  //滚动时事件
  scroll(e) {
    if (e.detail.scrollTop > 20) {
      this.data.isTop = false
    }
  },
  /**
   * 计算滑动角度
   * @param {Object} start 起点坐标
   * @param {Object} end 终点坐标
   */
  angle(start, end) {
    var _X = end.X - start.X,
      _Y = end.Y - start.Y
    //返回角度 /Math.atan()返回数字的反正切值
    return 360 * Math.atan(_Y / _X) / (2 * Math.PI);
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  refirsh: function() {
    this.refirshMoreList()
    this.refirshMoreRecomend()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return {
      title: '我新发现了一个有趣社团，我在这里等你哟',
      imgUrl: 'https://aa-1257564155.cos.ap-chengdu.myqcloud.com/tmp_955d50b786dc9776a422b990a07f26cc.jpg',
      path: '/pages/login/login?id=123'
    }
  },
  // 处理列表里面播放视频
  playvideo(e) {
    console.log(e)
  },

  goto(e) {
    wx.navigateTo({
      url: e.currentTarget.dataset.url,
    })
  },
  //获取社团列表
  getList(params, bool) {
    wx.teamApi.association.list(params).then(res => {
      if (bool) {
        this.setData({
          asslist: res.data.result
        })
      } else {
        if (res.data.result.length <= 0) {
          this.data.params.page -= 1
          wx.showToast({
            title: '我也是有底线的！',
            icon:'none'
          })
        }
        this.setData({
          asslist: this.data.asslist.concat(res.data.result),
          isLoodingHide:true
        })
      }

    })
  },

  gotoAccDetail(e) {
    wx.navigateTo({
      url: '../../pages/associationDetails/associationDetails?id=' + e.currentTarget.dataset.item.id,
    })
  },
  gotoInforDetail(e) {
    let data = e.currentTarget.dataset.item
    wx.navigateTo({
      url: '../../pages/newsDetail/index?id=' + data.id + '&type=' + data.newsType,
    })
  },
  //获取推荐列表
  getRecomend(params, bool) {
    wx.teamApi.association.recommendnews(this.data.recommonParams).then(res => {
      if (bool) {
        this.setData({
          recommonList: res.data.result
        })
      } else {
        if (res.data.result.length <= 0) {
          this.data.recommonParams.page -= 1
        }
        this.setData({
          recommonList: this.data.recommonList.concat(res.data.result),
          isLoodingHide:true
        })
      }
    })
  },
  //scroll-view滚动触底事件
  loadMoreList(e) {
    this.setData({
      isLoodingHide:false
    })
    this.data.params.page += 1
    this.getList(this.data.params)
  },
  //点赞事件
  zanClick(e) {
    let index = e.currentTarget.dataset.index
    let item = e.currentTarget.dataset.item
    if (item.upStatus) {
      this.cancelUpNews(item.id, index)
    } else {
      this.upNews(item.id, index)
    }
  },
  upNews(id, index) {
    wx.teamApi.news.upNews(id).then(res => {
      if (res.data.errcode === 0) {
        this.data.recommonList[index].upStatus = 1
        this.data.recommonList[index].upNum += 1
        this.setData({
          recommonList: this.data.recommonList
        })
      }
    })
  },
  cancelUpNews(id, index) {
    wx.teamApi.news.cancelUpNews(id).then(res => {
      if (res.data.errcode === 0) {
        this.data.recommonList[index].upStatus = 0
        this.data.recommonList[index].upNum -= 1
        this.setData({
          recommonList: this.data.recommonList
        })
      }
    })
  },
})